<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="所属社区">
              <j-select-depart placeholder="请选择所属社区" v-model="queryParam.community" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="小区名称">
              <a-input placeholder="请输入小区名称" v-model="queryParam.townName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-popover placement="top" trigger="click">
        <template #content>
          <a-select
            placeholder="请选择调查类型"
            v-model="importHouseType"
            @change="handleChangeImportHouseType"
            style="width: 160px"
            allowClear
          >
            <a-select-option value="1">生活小区</a-select-option>
            <a-select-option value="2" disabled>工业园区</a-select-option>
            <a-select-option value="3">其他类</a-select-option>
          </a-select>
        </template>
        <a-button type="primary" icon="import" @click="importHouseType = undefined">唐先入户调查导入</a-button>
      </a-popover>
      <a-button type="primary" icon="export" @click="handleExportXls('入户调查导出')">入户调查导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="record.id"
            height="25px"
            alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a @click="exportPDF(record)">报告生成</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <town-modal ref="modalForm" @ok="modalFormOk"></town-modal>

    <!-- pdf 导出报告 -->
    <div style="position:absolute; top: 0; left: 0">
      <townPDFReport :town-id="selectTownId" ref="townPDFReport" />
    </div>
    <a-upload
      v-show="false"
      name="file"
      :showUploadList="false"
      :multiple="false"
      :headers="tokenHeader"
      :action="importTxrhdcExcelUrl"
      @change="handleImportExcel"
    >
      <a-button ref="upload" type="primary">入户调查导入</a-button>
      <!--      <a-range-picker-->
      <!--        :show-time="{ format: 'HH:mm' }"-->
      <!--        format="YYYY-MM-DD HH:mm"-->
      <!--        :placeholder="['Start Time', 'End Time']"-->
      <!--        @change="onChange"-->
      <!--        @ok="onOk"-->
      <!--      />-->
    </a-upload>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TownModal from '@views/modules/Town/TownModal'
import townPDFReport from '@/views/components/Town/town-pdf-report.vue'

export default {
  name: 'TownList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    TownModal,
    townPDFReport
  },
  data() {
    return {
      description: '小区-官网数据表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: (t, r, index) => {
            const { pageSize, current } = this.ipagination
            return (current - 1) * pageSize + parseInt(index) + 1
          }
        },
        {
          title: '所属区域',
          align: 'center',
          dataIndex: 'areaId_dictText'
        },
        {
          title: '小区名称',
          align: 'center',
          dataIndex: 'townName'
        },
        {
          title: '所属社区',
          align: 'center',
          dataIndex: 'community_dictText'
        },
        {
          title: '小区地址',
          align: 'center',
          dataIndex: 'position'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/paishui/town/list',
        delete: '/paishui/town/delete',
        deleteBatch: '/paishui/town/deleteBatch',
        exportXlsUrl: '/paishui/town/exportXls',
        importExcelUrl: 'paishui/town/importExcel',
        importTxrhdcExcelUrl: 'paishui/import/excel/houseIns'
      },
      dictOptions: {},
      superFieldList: [],
      selectTownId: null,
      importHouseType: undefined
    }
  },
  created() {
    this.getSuperFieldList()
  },
  // 测试
  // mounted() {
  //   this.selectTownId = '1656944809208414210'
  // },
  computed: {
    importTxrhdcExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importTxrhdcExcelUrl}/${this.importHouseType}`
    }
  },
  methods: {
    handleChangeImportHouseType(type) {
      if (!type) {
        return
      }
      this.$nextTick(() => {
        console.log(this.importTxrhdcExcelUrl)
        this.$refs.upload.$el.click()
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'sel_depart', value: 'areaId', text: '所属区域' })
      fieldList.push({ type: 'string', value: 'townName', text: '小区名称', dictCode: '' })
      fieldList.push({ type: 'sel_depart', value: 'community', text: '所属社区' })
      fieldList.push({ type: 'string', value: 'position', text: '小区地址', dictCode: '' })
      this.superFieldList = fieldList
    },
    handleDetail(row) {
      this.$router.push({
        path: '/hcsjmanage/TownList/Detail',
        query: {
          id: row.id
        }
      })
    },
    exportPDF(record) {
      this.selectTownId = record.id
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
